package com.example.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("59.110.23.211");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("cmlstudy");
        //创建连接
        Connection connection = factory.newConnection();
        Channel channel=connection.createChannel();
        channel.queueDeclare("hello",true,false,false,null);
        //发送消息
        for (int i = 0; i <10 ; i++) {
            String message="hello rabbitmq"+i;
            channel.basicPublish("","hello",null,message.getBytes());
        }
        System.out.println("消息发送成功");
        channel.close();
        connection.close();

    }
}
